﻿using MediatR;
using W.Warehouse.Api.Application.Command.BinLocationCommand;
using W.Warehouse.Domain;
using W.Warehouse.Read.Api;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Api.Application.CommandHandlder.BinLocationCommandHandlder
{
    public class BinLocationFanCommandHandlder : IRequestHandler<BinLocationFanCommand, ApiResult<BinLocation>>
    {
        private readonly ILogger<BinLocationFanCommandHandlder> _logger;
        private readonly IBinLocationRepository _binLocationRepository;
        public BinLocationFanCommandHandlder(IBinLocationRepository binLocationRepository, ILogger<BinLocationFanCommandHandlder> logger)
        {
            _binLocationRepository = binLocationRepository;
            _logger = logger;
        }
        public async Task<ApiResult<BinLocation>> Handle(BinLocationFanCommand request, CancellationToken cancellationToken)
        {
            ApiResult<BinLocation> result = new ApiResult<BinLocation>();
            try
            {
                _logger.LogInformation("反填库位管理成功");
                var res = await _binLocationRepository.GetAsync(x => x.BinLocationId == request.binLocationId);
                result.Data = res.FirstOrDefault();
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
                throw;
            }
            return result;
        }
    }
}
